package com.km.app.service;

import com.km.app.dao.SysCompanyMapper;
import com.km.app.dao.SysFileDirectoryMapper;
import com.km.app.dao.SysFileMapper;
import com.km.app.domain.po.SysFile;
import com.km.app.domain.po.SysFileDirectory;
import com.km.app.util.IdGen;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: eric
 * Date: 2017-11-23
 * Time: 19:52
 */
@Service
@Transactional
public class FileService {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileService.class);

    @Autowired
    private SysFileMapper sysFileMapper;
    @Autowired
    private SysFileDirectoryMapper sysFileDirectoryMapper;
    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    public SysFile queryByMd5(String md5){
       return sysFileMapper.queryByMd5(md5);
    }

    public String saveVirtualPath(String appId, String path) {
        if (StringUtils.isBlank(path)) {
            return null;
        }
        SysFileDirectory vo = new SysFileDirectory();
        String parentId = "";
        String parentIds = "";
        String parentNames = "";
        String[] arr = path.split("\\/");
        for (String name : arr) {
            if (StringUtils.isNotBlank(name)) {
                name = name.trim();
                vo = sysFileDirectoryMapper.queryByName(appId, parentNames, name);
                if (vo != null) {
                    parentId = vo.getParentId();
                    parentIds = vo.getParentIds() + vo.getId() + ",";
                    parentNames = vo.getParentNames() + vo.getName() + ",";
                    continue;
                }
                vo = new SysFileDirectory();
                vo.setId(IdGen.uuid());
                vo.setAppId(appId);
                vo.setParentId(parentId);
                vo.setParentIds(parentIds);
                vo.setParentNames(parentNames);
                vo.setName(name);
                vo.setSort(0L);
                vo.setDescription("");
                vo.setCreateDate(new Date());
                vo.setDelFlag("0");
                sysFileDirectoryMapper.insertSelective(vo);
                parentId = vo.getParentId();
                parentIds = vo.getParentIds() + vo.getId() + ",";
                parentNames = vo.getParentNames() + vo.getName() + ",";
            }
        }
        return vo.getId();
    }

    public int saveFile(String id, String appId, String directoryId, String name, long size, String realPath, String virtualPath, String md5) {
        SysFile vo = new SysFile();
        vo.setId(id);
        vo.setAppId(appId);
        vo.setDirectoryId(directoryId);
        vo.setName(name);
        vo.setSize(size);
        vo.setPath(realPath);
        vo.setVirtualPath(virtualPath);
        vo.setMd5Code(md5);
        vo.setCreateDate(new Date());
        vo.setDelFlag("0");
        return sysFileMapper.insertSelective(vo);
    }

    public int deleteExistVirtualPathData(String virtualPath) {
        return sysFileMapper.deleteExistVirtualPathData(virtualPath);
    }

    public SysFile queryByVirtualPath(String fullPath) {
        return sysFileMapper.queryByVirtualPath(fullPath);
    }
}
